package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.WalletMainEntity;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class MyWalletActivity extends BaseActivity {

	public static int REQUEST_DRAW_MONEY = 1;
	
	private PullToRefreshScrollView scroolView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_my_wallet);
		super.onCreate(savedInstanceState);
		initView();
		getdata();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "钱包 ");
		showView(R.id.next_sure, true);
		setTvText(R.id.next_sure, "提现");
		
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.next_sure).setOnClickListener(this);
		findViewById(R.id.ly_draw_log).setOnClickListener(this);
		findViewById(R.id.ly_draw_accounts).setOnClickListener(this);
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements OnRefreshListener<ScrollView>{
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}

	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		API.http_post(this, API.WALLET_MAIN, params, new HttpCallback<WalletMainEntity>(this) {
			@Override
			public void onSuccess(int statusCode, WalletMainEntity data) {
				scroolView.onRefreshComplete();
				setTvText(R.id.tv_balance, ""+StringUtil.getMoneyString(data.getBalance()));
				setTvText(R.id.tv_unsettled, "待结算金额："+StringUtil.getMoneyString(data.getUnsettled()));
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				
//				WalletMainEntity data_tmp = new WalletMainEntity(35131, 5876545);
//				setTvText(R.id.tv_balance, ""+ StringUtil.getMoneyString(data_tmp.getBalance()));
//				setTvText(R.id.tv_unsettled, "待结算金额："+StringUtil.getMoneyString(data_tmp.getUnsettled()));
				
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<WalletMainEntity>() {};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.next_sure:
				startActivityForResult(new Intent(mActivity, MyWalletDrawActivity.class), REQUEST_DRAW_MONEY);
				break;
			case R.id.ly_draw_log:
				startActivity(new Intent(mActivity, MyWalletDrawLogActivity.class));
				break;
			case R.id.ly_draw_accounts:
				startActivity(new Intent(mActivity, MyWalletAccountActivity.class));
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return;
		if(requestCode==REQUEST_DRAW_MONEY){
			getdata();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}
